//
//  DFToolUtil.m
//  DFWeChatView
//
//  Created by Allen Zhong on 15/10/3.
//  Copyright (c) 2015年 Datafans, Inc. All rights reserved.
//

#import "DFToolUtil.h"
#import <CommonCrypto/CommonDigest.h>

@implementation DFToolUtil


+(NSString *)md5:(NSString *)str
{
    if (str == nil) {
        return @"";
    }
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];

}


+(NSString *)preettyTime:(NSString *)ts
{
    
    if (ts == nil) {
        return @"";
    }
   //原有时间
    NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    [dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//可能需要设置时区，此处设为东8即北京时间
    NSArray *firstDateStrArr=[ts componentsSeparatedByString:@" "];
    NSString *oldDateStr = [firstDateStrArr objectAtIndex:0];
    NSArray *oldDateArr = [oldDateStr componentsSeparatedByString:@"-"];
    //现在时间
    NSDate *currentDate = [NSDate date];
    NSDateComponents *componentsNow = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
    NSString *nowDateStr = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[componentsNow year], (long)[componentsNow month], (long)[componentsNow day]];
    NSArray *nowDateStrArr = [nowDateStr componentsSeparatedByString:@"-"];
    
    NSDateFormatter * dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *currentDateString = [dateFormatter2 stringFromDate:currentDate];
    NSDate *date1=[dateFormatter1 dateFromString:ts];
    NSDate *date2=[dateFormatter1 dateFromString:currentDateString];
    if (date1==nil) {
        return @"";
    }
    //同年
    if ([oldDateArr[0] intValue] == [nowDateStrArr[0] intValue]){
        
        //当天
        if( [oldDateArr[1] intValue] == [nowDateStrArr[1] intValue] && [nowDateStrArr[2] intValue]== [oldDateArr[2] intValue]){

            NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
           
            int hour = (int)(time/3600);
            int minute = (int)(time - hour*3600)/60;
            int second = time - hour*3600 - minute*60;
            
            NSString *dateContent = @"";
            
            if (hour>0) {
                dateContent=[[NSString alloc] initWithFormat:@"%i小时前",hour];
            }else if (minute>0){
                dateContent = [[NSString alloc]initWithFormat:@"%i分钟前",minute];
            }else if (second>0){
                dateContent = [[NSString alloc]initWithFormat:@"%i秒前",second];
            }else{
                dateContent = @"刚刚";
            }
            
            return [NSString stringWithFormat:@"%@", dateContent];
        }
        
        //昨天~30天以前
        if( [oldDateArr[1] intValue]==[nowDateStrArr[1] intValue]){
    
            if (([nowDateStrArr[2] intValue]-[oldDateArr[2] intValue]==1)) {
                return @"昨天";
            }else{
            
               int day = [nowDateStrArr[2] intValue]-[oldDateArr[2] intValue];
                return [[NSString alloc]initWithFormat:@"%i天前",day];
            }
        }else{
            //大于30天以前
            NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
            [dateFormatter1 setDateFormat:@"YYYY/MM/dd HH:mm:ss"];
             NSDate *date1=[dateFormatter1 dateFromString:ts];
            NSString *strDate = [dateFormatter1 stringFromDate:date1];
            return strDate;
        }
        
    }else{
        //大于一年前
        NSDateFormatter * dateFormatter1 = [[NSDateFormatter alloc] init];
        [dateFormatter1 setDateFormat:@"YYYY/MM/dd HH:mm:ss"];
        NSDate *date1=[dateFormatter1 dateFromString:ts];
        NSString *strDate = [dateFormatter1 stringFromDate:date1];
        return strDate;
        
    }
}


@end
